#include "pch.h"
#include "CalculateCamera.h"
#include "MathFoundation.h"
namespace mini
{
	namespace CameraCalculate
	{
		CCalculate::CCalculate()
		{
		}

		void CCalculate::mouseWheel(event& msg)
		{
			CVector result;
			if (MOUSEWHEELUP == msg.Messtype) 
			{
				result = *mpEyePt - *mpDirection;
			}
			else
			{
				result = *mpEyePt + *mpDirection;
			}
			if (D3DXVec3Length(&D3DXVECTOR3(&result.x)) > 2)
			{
				CVector lResult = result - *mpLookat;
				*mpEyePt = result;
				//*mpDirection = *mpEyePt - *mpLookat;
				D3DXVec3Normalize(mpDirection, mpDirection);
			}
		}


		//////////////////////////////////////////////////////////////////////////

		//////////////////////////////////////////////////////////////////////////
		CFirstPerson::CFirstPerson()
		{
			mType = CamermType::First;
		}

		CFirstPerson::~CFirstPerson()
		{

		}

		void CFirstPerson::left()
		{

		}

		void CFirstPerson::right()
		{

		}

		void CFirstPerson::front()
		{

		}

		void CFirstPerson::back()
		{

		}

		void CFirstPerson::rotation()
		{

		}


		void CFirstPerson::mouseWheel(event& msg)
		{
			CCalculate::mouseWheel(msg);
		}


		void CFirstPerson::mouseMove(event& msg)
		{
			CCalculate::mouseMove(msg);
			//if (0 != mMoustStatus)
				//{
				//	mbDirty = true;
				//	if (2 == mMoustStatus)
				//	{
				//		CMatrix matrixX;
				//		D3DXMatrixRotationX(&matrixX, 0.001f * (pt.y - msg.point.y));

				//		D3DXVECTOR4 r(dirLookAt, 1);
				//		D3DXVec4Transform(&r, &r, &matrixX);
				//		mLookat.x = r.x + mEyePt.x;
				//		mLookat.y = r.y + mEyePt.y;
				//		mLookat.z = r.z + mEyePt.z;				
				//	}
				//	else if(1 == mMoustStatus )
				//	{
				//		CMatrix matrixY;
				//		D3DXMatrixRotationY(&matrixY, 0.001f * (pt.x - msg.point.x));
				//		D3DXVECTOR4 r(dirEye, 1);
				//		D3DXVec4Transform(&r, &r, &matrixY);
				//		mEyePt.x = r.x + offsetLookAt.x;
				//		mEyePt.y = r.y + offsetLookAt.y;
				//		mEyePt.z = r.z + offsetLookAt.z;
				//	}
				//}
		}

		//////////////////////////////////////////////////////////////////////////
		CThirdPerson::CThirdPerson()
		{
			mType = CamermType::Third;
		}

		CThirdPerson::~CThirdPerson()
		{

		}

		void CThirdPerson::left()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			D3DXMatrixRotationY(&matrix, (float)D3DX_PI / 180.0f * 270.0f);
			D3DXVECTOR4 r(result, 1);
			D3DXVec4Transform(&r, &r, &matrix);
			mpMoveDirection->x = r.x;
			mpMoveDirection->y = r.y;
			mpMoveDirection->z = r.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CThirdPerson::right()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			D3DXMatrixRotationY(&matrix, (float)D3DX_PI / 180.0f * 90.0f);
			D3DXVECTOR4 r(result, 1);
			D3DXVec4Transform(&r, &r, &matrix);
			mpMoveDirection->x = r.x;
			mpMoveDirection->y = r.y;
			mpMoveDirection->z = r.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CThirdPerson::front()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			mpMoveDirection->x = result.x;
			mpMoveDirection->y = 0;
			mpMoveDirection->z = result.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CThirdPerson::back()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			mpMoveDirection->x = result.x;
			mpMoveDirection->y = 0;
			mpMoveDirection->z = result.z;
			*mpEyePt += (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat += (*mpMoveDirection * (*mpMoveSpeed));
		}


		void CThirdPerson::rotation()
		{
			D3DXVECTOR3 eye;
			eye.x = mpEyePt->x - mpLookat->x;
			eye.y = mpEyePt->y - mpLookat->y;
			eye.z = mpEyePt->z - mpLookat->z;

			float fvalue = D3DXVec3Length(&eye);

			eye.x = cos(mRotateX) * sin(mRotateY) * fvalue;
			eye.y = sin(mRotateX) * fvalue;
			eye.z = -cos(mRotateX) * cos(mRotateY) * fvalue;

			*mpEyePt = eye + *mpLookat;
		}

		void CThirdPerson::mouseWheel(event& msg)
		{
			CCalculate::mouseWheel(msg);
		}


		void CThirdPerson::mouseMove(event& msg)
		{
			if (::GetAsyncKeyState(VK_RBUTTON))
			{
				if (-1 == mpClickPt->x)
				{
					mpClickPt->x = msg.point.x;
					mpClickPt->y = msg.point.y;
				}
				if (msg.point.y - mpClickPt->y != 0 ||
					msg.point.x - mpClickPt->x != 0)
				{

					mRotateY += ((msg.point.x - mpClickPt->x) / 1380.0f);
					mRotateX += ((msg.point.y - mpClickPt->y) / 1380.0f);
					mini::Clamp(mRotateX, 0.0f, 1.57f);
					rotation();
					mpClickPt->x = msg.point.x;
					mpClickPt->y = msg.point.y;
				}
			}
			else
			{
				mpClickPt->x = -1;
			}
		}

		//////////////////////////////////////////////////////////////////////////
		CFree::CFree()
		{
			mType = CamermType::Free;
		}

		CFree::~CFree()
		{

		}

		void CFree::left()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			D3DXMatrixRotationY(&matrix, (float)D3DX_PI / 180.0f * 270.0f);
			D3DXVECTOR4 r(result, 1);
			D3DXVec4Transform(&r, &r, &matrix);
			mpMoveDirection->x = r.x;
			mpMoveDirection->y = r.y;
			mpMoveDirection->z = r.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CFree::right()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			D3DXMatrixRotationY(&matrix, (float)D3DX_PI / 180.0f * 90.0f);
			D3DXVECTOR4 r(result, 1);
			D3DXVec4Transform(&r, &r, &matrix);
			mpMoveDirection->x = r.x;
			mpMoveDirection->y = r.y;
			mpMoveDirection->z = r.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CFree::front()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			mpMoveDirection->x = result.x;
			mpMoveDirection->y = result.y;
			mpMoveDirection->z = result.z;
			*mpEyePt -= (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat -= (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CFree::back()
		{
			CVector result = *mpEyePt - *mpLookat;
			D3DXVec3Normalize(&result, &result);
			CMatrix	matrix;

			mpMoveDirection->x = result.x;
			mpMoveDirection->y = result.y;
			mpMoveDirection->z = result.z;
			*mpEyePt += (*mpMoveDirection * (*mpMoveSpeed));
			*mpLookat += (*mpMoveDirection * (*mpMoveSpeed));
		}

		void CFree::rotation()
		{
			D3DXVECTOR3 lookat;
			lookat.x = mpEyePt->x - mpLookat->x;
			lookat.y = mpEyePt->y - mpLookat->y;
			lookat.z = mpEyePt->z - mpLookat->z;

			float fvalue = D3DXVec3Length(&lookat);

			lookat.x = cos(mRotateX) * sin(mRotateY) * fvalue;
			lookat.y = sin(mRotateX) * fvalue;
			lookat.z = cos(mRotateX) * cos(mRotateY) * fvalue;

			*mpLookat = lookat + *mpEyePt;

		}

		void CFree::mouseWheel(event& msg)
		{
			CCalculate::mouseWheel(msg);
		}

		void CFree::mouseMove(event& msg)
		{
			if (::GetAsyncKeyState(VK_RBUTTON) )
			{
				if (-1 == mpClickPt->x )
				{
					mpClickPt->x = msg.point.x;
					mpClickPt->y = msg.point.y;
				}
				if (msg.point.y - mpClickPt->y != 0 ||
					msg.point.x - mpClickPt->x != 0)
				{
					mRotateY += ((msg.point.x - mpClickPt->x) / 380.0f);
					mRotateX += ((msg.point.y - mpClickPt->y) / 380.0f);
				//	mini::Clamp(mRotateX, 0.0f, 1.57f);
					rotation();
					mpClickPt->x = msg.point.x;
					mpClickPt->y = msg.point.y;
				}
			}
			else
			{
				mpClickPt->x = -1;
			}
		}
	}
}
